package cn.wzvtc.demo.controller;

import cn.wzvtc.demo.common.dto.Result;
import cn.wzvtc.demo.model.entity.FormTplEntity;
import cn.wzvtc.demo.service.FormTplService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 模板表单控制器
 * @author: Rex
 * @create: 2021-07-10 18:51
 */
@RestController
public class FormTplController {

  @Autowired
  FormTplService formTplService;


  /**
   * 添加数据
   * @param formTplEntity 表单实体类
   * @return 处理结果
   */
  @PostMapping(value = "/form")
  public Result<FormTplEntity> add(@RequestBody FormTplEntity formTplEntity) {
    FormTplEntity res = formTplService.save(formTplEntity);
    System.out.println(res);
    return Result.success(res);
  }

  /**
   * 删除整个表单
   * @param id 表单编号
   * @return 处理结果
   */
  @DeleteMapping(value = "/form")
  public Result<Void> deleteById(String id) {
    formTplService.deleteById(id);
    return Result.success("删除成功");
  }

  /**
   * 根据id和component_id删除组件
   * @param id 模板表单id
   * @param componentId 组件表单id
   * @return 处理结果
   */
  @DeleteMapping(value = "/form/component")
  public Result<Void> deleteByIdAndComponentId(String id,String componentId) {
    formTplService.deleteByIdAndComponentId(id,componentId);
    return Result.success("删除成功");
  }


  /**
   * 根据表单模板编号查询表单模板信息
   * @param id 模板表单id
   * @return 处理结果
   */
  @GetMapping(value = "/form")
  public Result<FormTplEntity> findById(String id) {
    FormTplEntity formTplEntity = formTplService.findById(id);
    System.out.println(formTplEntity);
    return Result.success(formTplEntity);
  }

  /**
   * 根据ID更新数据
   * @param formTplEntity 待更新表单
   * @return 处理结果
   */
  @PutMapping(value = "/form")
  public Result<FormTplEntity> updateById(@RequestBody FormTplEntity formTplEntity) {
    formTplService.update(formTplEntity);
    System.out.println(formTplEntity);
    return Result.success(formTplEntity);
  }
}
